//
//  ShareView.swift
//  Scanread
//
//  Created by Macintosh on 2023/6/23.
//

import UIKit
import SnapKit

class ShareView: UIView {

    var shareItemBlock: ((Int)->Void)?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(valueRGB: 0x000000, alpha: 0.1)
        
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setupUI() {
        let contentView = UIView()
        contentView.backgroundColor = UIColor(valueRGB: 0xF4F4FA, alpha: 1.0)
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.height.equalTo(220 + kXBottomHeight)
        }
        
        contentView.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(15)
            make.centerX.equalToSuperview()
        }
        
        contentView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(0)
            make.height.equalTo(52 + kXBottomHeight)
        }
        
        let wechatView = platformItemView("微信", "account_wechat_icon", 0)
        contentView.addSubview(wechatView)
        wechatView.snp.makeConstraints { make in
            make.left.equalTo(80)
            make.width.equalTo(80)
            make.height.equalTo(80)
            make.top.equalTo(titleLabel.snp.bottom).offset(20)
        }
        
        let circleView = platformItemView("微信朋友圈", "wechat_friends_icon", 1)
        contentView.addSubview(circleView)
        circleView.snp.makeConstraints { make in
            make.right.equalTo(-80)
            make.width.equalTo(80)
            make.height.equalTo(80)
            make.top.equalTo(wechatView)
        }
    }
    
    @objc func actionTap(_ tap: UITapGestureRecognizer) {
        let index = tap.view!.tag - 1000
        if let block = shareItemBlock {
            block(index)
            removeFromSuperview()
        }
    }
    
    func platformItemView(_ title: String, _ imageName: String, _ index: Int) -> UIView {
        let item = UIView()
        item.tag = 1000 + index
        let tap = UITapGestureRecognizer(target: self, action: #selector(actionTap(_:)))
        item.addGestureRecognizer(tap)
        
        let iconIV = UIImageView()
        iconIV.image = UIImage(named: imageName)
        item.addSubview(iconIV)
        iconIV.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.centerX.equalToSuperview()
            make.height.width.equalTo(50)
        }
        
        let titleLabel = UILabel()
        titleLabel.text = title
        titleLabel.textColor = UIColor.black
        titleLabel.font = UIFont.systemFont(ofSize: 14)
        titleLabel.textAlignment = .center
        item.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
        }
        
        return item
    }
    
    lazy var titleLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.text = "选择要分享到的平台"
        titleLabel.textColor = UIColor.black
        titleLabel.font = UIFont.systemFont(ofSize: 16)
        
        return titleLabel
    }()
    
    lazy var cancelBtn: UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.white
        btn.setTitle("取消分享", for: .normal)
        btn.setTitleColor(UIColor.black, for: .normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        btn.addTarget(self, action: #selector(actionCancel), for: .touchUpInside)
        
        return btn
    }()
    
    @objc func actionCancel() {
        removeFromSuperview()
    }

}
